/* @(#)DownloadFileVersion.java    V1.0
 *
 * Project: APEP 
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *       {CREATE_TIME} First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd. 
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *   
 * 
 */
package com.apep.gw.synch;

import java.io.PrintWriter;

import com.apep.gw.data.DownloadFileVersionPageData;

import framework.core.DBConnection;
import framework.core.IHandler;
import framework.core.ILoginContext;
import framework.core.Logger;
import framework.core.ObjectList;
import framework.core.PageData;
import framework.core.SearchEvent;
import framework.core.SrvContext;

/*
 * Author : CreateTime:{CREATE_TIME}
 */
public class DownloadFileVersionSrv extends IHandler {

    public DownloadFileVersionSrv() {
	super();
    }

    private void listDownloadFileVersion(DBConnection con, SrvContext context,
	    String path) throws Exception {
	DownloadFileVersionSearchEvent event = (DownloadFileVersionSearchEvent) SearchEvent
		.fromForm(context, DownloadFileVersionSearchEvent.class);
	context.setSessionValue("DownloadFileVersionSearchEvent", event);
	context.setNextUrl(path + "DownloadFileVersionList.jsp");
    }

    private void backDownloadFileVersion(DBConnection con, SrvContext context,
	    String path) throws Exception {
	context.setNextUrl(path + "DownloadFileVersionList.jsp");
    }

    public void doAction(DBConnection con, SrvContext context,
	    ILoginContext loginContext) throws Exception {
	String action_code = context.getActionCode();
	String path = "";

	if (action_code.equalsIgnoreCase("listDownloadFileVersion")) {
	    listDownloadFileVersion(con, context, path);
	    return;
	}

	if (action_code.equalsIgnoreCase("toAddDownloadFileVersion")) {
	    DownloadFileVersion obj = DownloadFileVersion.fetch();
	    DownloadFileVersionPageData data = (DownloadFileVersionPageData) PageData
		    .fromTable(obj, DownloadFileVersionPageData.class);
	    context.setValue("DownloadFileVersionPageData", data);
	    context.setNextUrl(path + "DownloadFileVersionEdit.jsp");
	    return;
	}

	if (action_code.equalsIgnoreCase("backListDownloadFileVersion")) {
	    backDownloadFileVersion(con, context, path);
	    return;
	}
	
	if(action_code.equalsIgnoreCase("requestVersion")) {
	    int major = context.getInt("Major");
	    int minor = context.getInt("Minor");
	    int revision = context.getInt("Revision");
	    ObjectList list = DownloadFileVersion.getListByVersion(con, major, minor, revision);
	    String tXMLStr = DownloadFileVersion.toXML(list);
	    PrintWriter tOutput = context.m_res.getWriter();
	    tOutput.write(tXMLStr);
	    return;
	}
	
	// add by start
    if(action_code.equalsIgnoreCase("requestDownload")) {
        
        PrintWriter tOutput = context.m_res.getWriter();
        tOutput.write("OK");
        return;
    }
	// add by end

	Logger.throwException("DownloadFileVersionSrv", "unknown action_code="
		+ action_code);
    }
}
